import { manualCheckIn } from './manualCheckIn.js'
import schedule from 'node-schedule'
import request from 'request'
const fileName = '.config.json';
import fs from 'node:fs'
import path from 'node:path'
const userDir = process.env.USERPROFILE || process.env.HOME;
const filePath = path.join(userDir, fileName);

const readFile = async (filePath) => {
  return new Promise((resolve, reject) => {
    try {
      fs.readFile(filePath, 'utf8', async (err, data) => {
        if (err) {
          console.error(`读取文件时出错: ${err}`);
          reject(err)
        } else {
          const jsonData = JSON.parse(data);
          console.log('读取到本地配置，开始签到')
          resolve(jsonData)
        }
      })
    } catch (err) {
      console.error(err);
      reject(err)
    }
  })
}
const signInApi = ({ check_url, url, cookie }) => {
  request(check_url, {
    method: 'post',
    headers: {
      Referer: url,
      Cookie: cookie
    },
  }, function (error, response, body) {
    if (!error && response.statusCode == 200) {
      console.log(body)
    }
  })
}
const init = async () => {
  const data = await readFile(filePath)
  console.log(data,'data12')
  var rule = new schedule.RecurrenceRule();

  rule.dayOfWeek = [0, new schedule.Range(1, 6)];

  rule.hour = +data.hour;

  rule.minute = +data.minute;
  console.log(rule,'rule')
  schedule.scheduleJob(rule, () => {
    signInApi(data)
    console.log(new Date().toString());
  });
}
init()
